package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.cucc.service.attr.OrderModelCalculateHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.PrivDiscountRateAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;

import java.text.DecimalFormat;
import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 物网协同折扣率属性联动
 *
 * @author LIFF
 */
@Service
@AttrHandler(PrivDiscountRateAttrHandler.class)
public class PrivDiscountRateAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Resource
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 物网协同结算价折扣率（%）
        KiteAttr attr19 = inst.getAttr("210008105");
        // PSM节电结算价折扣率（%）
        KiteAttr attr20 = inst.getAttr("210008106");
        DecimalFormat df = new DecimalFormat("#.###");

        if ("210008105".equals(attr.getFieldName())) {
            // 物网协同-物网关系绑定结算价格（万元）
            Double d = StringUtils.isEmpty(attr19.getValue()) ? 5 : Double.valueOf(attr19.getValue()) * 5 / 100;
            operateInstAttrService.setAttrValue(inst, "210008086", doubleTrans(df.format(d)), data.getSceneInstId(), null);

            // 物网协同-网络信息查询结算价格（万元）
            operateInstAttrService.setAttrValue(inst, "210008088", doubleTrans(df.format(d)), data.getSceneInstId(), null);

            // 物网协同-网络LBS服务结算价格（万元）
            operateInstAttrService.setAttrValue(inst, "210008090", doubleTrans(df.format(d)), data.getSceneInstId(), null);
        }

        if ("210008106".equals(attr.getFieldName())) {
            // PSM节电-NB设备节电开通和配置结算价格（万元）
            Double d = StringUtils.isEmpty(attr20.getValue()) ? 10 : Double.valueOf(attr20.getValue()) * 10 / 100;
            operateInstAttrService.setAttrValue(inst, "210008092", doubleTrans(df.format(d)), data.getSceneInstId(), null);

            // PSM节电-Cat.1设备PSM节电开通和配置结算价格（万元）
            operateInstAttrService.setAttrValue(inst, "210008095", doubleTrans(df.format(d)), data.getSceneInstId(), null);
        }

    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
    }

    private static String doubleTrans (String d){
        return OrderModelCalculateHelper.doubleTrans(d);
    }
}
